//
//  HeadTabBar.m
//  ebook
//
//  Created by 吴超飞 on 2016/11/25.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "HeadTabBar.h"

@interface HeadTabBar()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *itemViewArray;
@property (nonatomic, assign) NSInteger lastIndex; //上一次选择的index

@end

@implementation HeadTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.screenWidth , kHeadTabBarHeight)];
    if (self) {
        self.itemMainWith = 70;
        [self creatView];
    }
    return self;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self creatView];
}

- (void)creatView {
    
    CGRect frame = self.bounds;
    frame.size.width -= kHeadTabBarHeight;//加号是一个方形的按钮
    _scrollView = [[UIScrollView alloc] initWithFrame:frame];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_scrollView];
    
    UIButton *addBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_scrollView.frame), self.frame.origin.y, kHeadTabBarHeight, kHeadTabBarHeight)];
    [addBtn setImage:[UIImage imageNamed:@"add_icon"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:addBtn];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeadTabBarHeight-0.5, MTScreenW,0.5)];
    lineView.backgroundColor = MTConfigColor(@"MTStoreMenu_lineBg_Color");
    [self addSubview:lineView];
}

- (void)addBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(headTabBar:addBtnClick:)]) {
        [self.delegate headTabBar:self addBtnClick:btn];
    }
}

-(void)reloadData:(NSArray<ChannelUnitModel *> *)items currentChannel:(ChannelUnitModel *)channel{
    self.itemArray = items;
    self.currentChanel = channel;
    [self selectedChannel:channel];
}

- (void)setItemArray:(NSArray<ChannelUnitModel *> *)itemArray{
    _itemArray = itemArray;
    CGFloat itemWithd = self.screenWidth / itemArray.count;
    if (itemWithd > _itemMainWith) {
        _itemMainWith = itemWithd;
    }
    [self itemViewTextWithTexts:itemArray];
}

- (void)itemViewTextWithTexts:(NSArray *)textArray {
    
    for (UIView *sub in _scrollView.subviews) {
        [sub removeFromSuperview];
    }
    
    CGFloat width = 0;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:textArray.count];
    for (int i = 0; i < textArray.count; i++) {
        
        UIButton *itemView = [[UIButton alloc] init];
        itemView.tag = i;
        CGRect itemViewFrame = itemView.frame;
        itemViewFrame.size.width = self.itemMainWith;
        itemViewFrame.size.height = self.frame.size.height;
        itemViewFrame.origin.x = self.itemMainWith * i;
        itemView.frame = itemViewFrame;
        
        ChannelUnitModel *model = textArray[i];
        [itemView setTitle:model.Name forState:UIControlStateNormal];
        [itemView setTitle:model.Name forState:UIControlStateSelected];
        [itemView setTitleColor:MTConfigColor(@"MTStoreMenu_buttonTitleNormal_Color") forState:UIControlStateNormal];
        [itemView setTitleColor:MTConfigColor(@"MTStoreMenu_buttonTitleSelected_Color") forState:UIControlStateSelected];
        [itemView addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_scrollView addSubview:itemView];
        [array addObject:itemView];
        width += itemViewFrame.size.width;
    }
    
    _itemViewArray = array;
    _scrollView.contentSize = CGSizeMake(width, 0);
    self.currentChanel = [_itemArray firstObject];
}

- (CGFloat)screenWidth {
    return [UIScreen mainScreen].bounds.size.width;
}

- (void)setCurrentChanel:(ChannelUnitModel *)currentChanel {
    
    _currentChanel = currentChanel;
    NSInteger index = 0;
    UIButton *selectButton;
    for (int i=0;i<_itemArray.count;i++) {//获取当前选中项index，并设置选中态
        ChannelUnitModel *channel = [_itemArray objectAtIndex:i];
        UIButton *button = (UIButton *)_itemViewArray[i];
        button.selected = NO;
        if ([channel.ID isEqualToString:currentChanel.ID]) {
            index = i;
            button.selected = YES;
            selectButton = button;
        }
    }
    
    CGFloat offsetX = selectButton.frame.origin.x - self.screenWidth / 2;
    offsetX = offsetX > 0 ? (offsetX + _itemMainWith / 2) : 0;
    offsetX = offsetX > _scrollView.contentSize.width - _scrollView.frame.size.width ? _scrollView.contentSize.width - _scrollView.frame.size.width : offsetX;
    {
        if (index > _lastIndex) { //向右边移动
            [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        } else if (index < _lastIndex){ //向左边移动
            [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        }
    }
    
    _lastIndex = index;
}

- (void)buttonClick:(UIButton *)button {
    
    ChannelUnitModel *channel = [_itemArray objectAtIndex:button.tag];
    self.currentChanel = channel;
    [self selectedChannel:channel];
}

- (void)finishWithIndex:(NSInteger)index {

    if (index - 1 >= 0 && index - 1 < _itemArray.count) {
        index = index - 1;
    }else if (index + 1 >= 0 && index + 1 < _itemArray.count) {
        index = index + 1;
    }
    
    [self selectedChannel:[_itemArray objectAtIndex:index]];
}

- (void)selectedChannel:(ChannelUnitModel *)model{
    if([self.delegate respondsToSelector:@selector(headTabBar:didSelectedItem:)]){
        [self.delegate headTabBar:self didSelectedItem:model];
    }
}

@end
